#include <string>

#include "rw_lock.h"

int shareData = 0;

RWLock rw;
anotherWay::RWLock another;

void readFunc(int id) {
    while (true) {
        // rw.acquiredRead();
        another.rlock();
        std::string msg = "Read  thread " + std::to_string(id) +
                          ", is reading data: " + std::to_string(shareData) +
                          '\n';
        std::cout << msg;
        // rw.releaseRead();
        another.unlock();
        std::this_thread::sleep_for(std::chrono::microseconds(100));
    }
}

void writeFunc(int id) {
    while (true) {
        // rw.acquiredWrite();
        another.wlock();
        std::string msg = "Write thread " + std::to_string(id) +
                          ", is writing data: " + std::to_string(shareData) +
                          '\n';
        std::cout << msg;
        // rw.releaseWrite();
        another.unlock();
        std::this_thread::sleep_for(std::chrono::microseconds(100));
    }
}

int main() {
    std::thread reader[3];
    std::thread writer[2];

    for (int i = 0; i < 3; ++i) reader[i] = std::thread(readFunc, i + 1);
    for (int i = 0; i < 2; ++i) writer[i] = std::thread(writeFunc, i + 1);
    for (int i = 0; i < 3; ++i) reader[i].join();
    for (int i = 0; i < 2; ++i) writer[i].join();

    return 0;
}